import React, { useState } from 'react';
import {useHistory} from 'react-router-dom'
import {Input,Form,Button} from 'antd'
import axios from 'axios';

const FormItem = Form.Item
export default function Login (props) {
  
  const [name, setname] = useState('');
  const [pwd, setpwd] = useState('');
  const history = useHistory()
  console.log(useHistory());
  const loginbtn = async () => {
    const {data:res} = await login(name, pwd)
    console.log(res);
    if (res.code === 0) {
      localStorage.setItem("token",res.data.userName)
      props.history.push('/home')
    }
  }
  return <Form className='login-from'>
    <FormItem>
      <Input placeholder='请输入用户名' maxLength={5} onChange={(event) => {
        setname(event.target.value)
      }}/>
    </FormItem>
    <FormItem>
      <Input placeholder='请输入密码' maxLength={5} type="password" onChange={(event) => {
        setpwd(event.target.value)
      }}/>
    </FormItem>
    <label>{name}----{ pwd}</label>
    <FormItem>
      <Button type='primary' onClick={loginbtn}
        // onClick={() => {
        // login(name, pwd).then((res => {
        //   console.log(props);
        //   if (res.data.code === 0) {
        //     history.push('/home')
        //   }
        // }))
        // }}
      >登录</Button>
    </FormItem>
  </Form>
}

// 
function login(name, pwd) {
  return axios.get('/login.json', {
    params: {
      name,pwd
    }
  })
}